package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.CallUserDTO;
import com.hksj.Intelligentmanagement.entity.CallUserEntity;
import com.hksj.Intelligentmanagement.mapper.CallUserMapper;
import com.hksj.Intelligentmanagement.vo.CallUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CallUserService extends ServiceImpl<CallUserMapper, CallUserEntity> {

    @Autowired
    private CallUserMapper callUserMapper;

    //新增护工信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)//异常处理
    public Result createPorter(CallUserDTO callUserDTO) {
        //工号唯一性
        QueryWrapper<CallUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_no", callUserDTO.getEmployeeNo());
        Integer count = callUserMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error("工号已存在");
        }
        //手环登陆码唯一性
        QueryWrapper<CallUserEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("device_password", callUserDTO.getDevicePassword());
        Integer count1 = callUserMapper.selectCount(wrapper1);
        if (count1 > 0) {
            return Result.error("手环登陆码已存在");
        }
        CallUserEntity callUserEntity = new CallUserEntity();
        BeanUtils.copyProperties(callUserDTO, callUserEntity);
        //在表中创建一条记录
        int insert = callUserMapper.insert(callUserEntity);
        if (insert <= 0) {
            return Result.error("新增失败");
        }
        return Result.ok("新增成功");
    }

    //删除护工信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deletePorter(String id) {
        int insert = callUserMapper.deleteById(id);
        if (insert <= 0) {
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    //更新护工信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updatePorter(CallUserDTO callUserDTO) {
        CallUserEntity callUserEntity = new CallUserEntity();
        BeanUtils.copyProperties(callUserDTO, callUserEntity);
        int i = callUserMapper.updateById(callUserEntity);
        if (i <= 0) {
            return Result.error("更新失败");
        }
        return Result.ok("更新成功");
    }

    //根据id查询护工信息
    public Result queryPorterById(String id) {
        //调用selectById查询专业信息
        CallUserEntity entity = callUserMapper.selectById(id);
        //返回查询结果
        return Result.data(entity);
    }

    //模糊查询护工信息列表
    public IPage<CallUserVo> queryPorterListByPage(CallUserDTO callUserDTO) {
        //创建一个分页实例 mybatis plus的Page类型为IPage子类 能实现 对查询结果集排序
        Page<CallUserVo> page = new Page<>(callUserDTO.getPageNum(), callUserDTO.getPageSize());
        //在SQL查询结果集使用创建时间正序排序
        page.addOrder(OrderItem.asc("create_time"));
        //对查询进行分页
        List<CallUserVo> porterVos = callUserMapper.queryPorterListByPage(page, callUserDTO);
        //保存分页记录
        page.setRecords(porterVos);
        //返回分页
        return page;
    }

    public CallUserEntity getUserByDevicePassword(String userDevicePassword) {
        LambdaQueryWrapper<CallUserEntity> query = new QueryWrapper<CallUserEntity>().lambda()
                .eq(CallUserEntity::getDevicePassword, userDevicePassword)
                .eq(CallUserEntity::getState,0)
                .eq(CallUserEntity::getDelFlag,0);
        return callUserMapper.selectOne(query);
    }

    public CallUserEntity getUserByMac(String addr) {
        return lambdaQuery().eq(CallUserEntity::getMac,addr).one();
    }
}
